<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${basePackage}.repository.${entityName}Repository" >

    <resultMap id="BaseResultMap" type="${basePackage}.entity.${entityName}" >
    <#list columns as c>
        <#if c.columnKey == "PRI">
        <id column="${c.columnName}" property="${c.propertyName}" jdbcType="${c.jdbcType}" />
        <#else>
        <result column="${c.columnName}" property="${c.propertyName}" jdbcType="${c.jdbcType}" />
        </#if>
    </#list>
    </resultMap>


    <sql id="Base_Column_List" >
        ${columnList}
    </sql>


    <delete id="deleteByPrimaryKey" parameterType="${key.javaType}" >
        delete from `${tableName}`
        where `${key.columnName}` = ${prevChar}{${key.propertyName},jdbcType=${key.jdbcType}}
    </delete>


    <insert id="insert" parameterType="${basePackage}.entity.${entityName}" >
        insert into `${tableName}` (${columnList})
        values (<#list columns as c>${prevChar}{${c.propertyName},jdbcType=${c.jdbcType}}<#if c_has_next>,</#if></#list>)
    </insert>


    <insert id="insertSelective" parameterType="${basePackage}.entity.${entityName}" >
        insert into `${tableName}`
        <trim prefix="(" suffix=")" suffixOverrides="," >
        <#list columns as c>
            <#if c.entityType == "Integer">
            <if test="${c.propertyName} != null or ${c.propertyName} == 0" >
            <#else>
            <if test="${c.propertyName} != null" >
            </#if>
                `${c.columnName}`,
            </if>
        </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
        <#list columns as c>
            <#if c.entityType == "Integer">
            <if test="${c.propertyName} != null or ${c.propertyName} == 0" >
            <#else>
            <if test="${c.propertyName} != null" >
            </#if>
                ${prevChar}{${c.propertyName},jdbcType=${c.jdbcType}},
            </if>
        </#list>
        </trim>
    </insert>


    <update id="updateByPrimaryKeySelective" parameterType="${basePackage}.entity.${entityName}" >
        update `${tableName}`
        <set >
        <#list normalColumns as c>
            <#if c.entityType == "Integer">
            <if test="${c.propertyName} != null or ${c.propertyName} == 0" >
            <#else>
            <if test="${c.propertyName} != null" >
            </#if>
                `${c.columnName}` = ${prevChar}{${c.propertyName},jdbcType=${c.jdbcType}},
            </if>
        </#list>
        </set>
        where `${key.columnName}` = ${prevChar}{${key.propertyName},jdbcType=${key.jdbcType}}
    </update>


    <update id="updateByPrimaryKey" parameterType="${basePackage}.entity.${entityName}" >
        update `${tableName}` set
        <#list normalColumns as c>
        `${c.columnName}` = ${prevChar}{${c.propertyName},jdbcType=${c.jdbcType}}<#if c_has_next>,</#if>
        </#list>
        where `${key.columnName}` = ${prevChar}{${key.propertyName},jdbcType=${key.jdbcType}}
    </update>


    <select id="selectById" parameterType="java.lang.String" resultMap="BaseResultMap" >
        select
        <include refid="Base_Column_List" />
        from  `${tableName}`
        where `${key.columnName}` = ${prevChar}{${key.propertyName},jdbcType=${key.jdbcType}}
    </select>


    <sql id="findPageWithWhere" >
        <where> 1=1
        <#list columns as c>
            <#if c.entityType == "Integer">
            <if test="${c.propertyName} != null or ${c.propertyName} == 0" >
            <#else>
            <if test="${c.propertyName} != null" >
            </#if>
                and `${c.columnName}` = ${prevChar}{${c.propertyName},jdbcType=${c.jdbcType}}
            </if>
        </#list>

            <if test="null != likes">
                <foreach collection="likes.keys" item="k" separator=" ">
                <#list columns as c>
                    <if test='k == "${c.propertyName}" and likes[k] != null'>
                        and `${c.columnName}` like concat('%', ${prevChar}{likes.${c.propertyName},jdbcType=${c.jdbcType}}, '%')
                    </if>
                </#list>
                </foreach>
            </if>

            <if test="null != dates">
                <foreach collection="dates.keys" item="k" separator=" ">
                <#list columns as c>
                    <#if c.entityType == "Date">
                    <if test='k == "${c.propertyName}" and dates[k] != null'>
                        and ( `${c.columnName}` >= date(${prevChar}{dates.${c.propertyName}.begin})
                        and date(${prevChar}{dates.${c.propertyName}.end}) >= `${c.columnName}` )
                    </if>
                    </#if>
                </#list>
                </foreach>
            </if>

            <if test="null != includes">
                <foreach collection="includes.keys" item="k" separator=" ">
                <#list columns as c>
                    <if test='k == "${c.propertyName}" and includes[k] != null'>
                        and `${c.columnName}` in
                        <foreach item="itemValue" collection="includes[k]" separator="," open="(" close=")" index="index">
                            ${prevChar}{itemValue}
                        </foreach>
                    </if>
                </#list>
                </foreach>
            </if>
        </where>
    </sql>

    <sql id="findPageWithWhereLike" >
        <where> 1=1
        <#list columns as c>
            <#if c.entityType == "Integer">
            <if test="${c.propertyName} != null or ${c.propertyName} == 0" >
            <#else>
            <if test="${c.propertyName} != null" >
            </#if>
                and `${c.columnName}` like concat('%', ${prevChar}{${c.propertyName},jdbcType=${c.jdbcType}}, '%')
            </if>
        </#list>
        </where>
    </sql>


    <sql id="findPageWithOrder">
        <if test="null != orders" >
            ORDER BY
            <foreach collection="orders.keys" item="k" separator=",">
                <#list columns as c>
                <if test='k == "${c.propertyName}"'>
                    `${c.columnName}`
                </if>
                </#list>
                <if test='orders[k] == "asc"'>
                    ASC
                </if>
                <if test='orders[k] == "desc"'>
                    DESC
                </if>
            </foreach>
        </if>
    </sql>


    <select id="findOne" parameterType="${basePackage}.entity.${entityName}" resultMap="BaseResultMap" >
        select
        <include refid="Base_Column_List" />
        from  `${tableName}`
        <include refid="findPageWithWhere" />
        limit 1
    </select>


    <select id="findAllWithResult" parameterType="${basePackage}.entity.${entityName}" resultMap="BaseResultMap" >
        select
        <include refid="Base_Column_List" />
        from  `${tableName}`
        <include refid="findPageWithWhere" />
        <include refid="findPageWithOrder" />
    </select>


    <select id="findPageWithResult" parameterType="${basePackage}.entity.${entityName}" resultMap="BaseResultMap" >
        select
        <include refid="Base_Column_List" />
        from  `${tableName}`
        <include refid="findPageWithWhere" />
        <include refid="findPageWithOrder" />
        limit ${prevChar}{start},${prevChar}{pageSize}
    </select>


    <select id="findPageWithCount" parameterType="${basePackage}.entity.${entityName}" resultType="java.lang.Integer" >
        select count(1)
        from `${tableName}`
        <include refid="findPageWithWhere" />
    </select>

    <select id="findPageWithResultLike" parameterType="${basePackage}.entity.${entityName}" resultMap="BaseResultMap" >
        select
        <include refid="Base_Column_List" />
        from  `${tableName}`
        <include refid="findPageWithWhereLike" />
        <include refid="findPageWithOrder" />
        limit ${prevChar}{start},${prevChar}{pageSize}
    </select>


    <select id="findPageWithCountLike" parameterType="${basePackage}.entity.${entityName}" resultType="java.lang.Integer" >
        select count(1)
        from `${tableName}`
        <include refid="findPageWithWhereLike" />
    </select>


</mapper>